﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace UI_PMQLThuPhi_LMD
{
    class AppConfigFileSetting
    {
        public static void UpdateConnectionStringValue(string newValue)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
                    
            // Attempt to locate the requested setting.
            foreach (XmlElement xmlElementObj in xmlDoc.DocumentElement)
            {
                if (xmlElementObj.Name == "connectionStrings")
                    foreach (XmlNode xmlNodeObj in xmlElementObj.ChildNodes)
                    {
                        xmlNodeObj.Attributes["connectionString"].Value = newValue;
                    }
            }
            
            xmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
        }

        public static void UpdateAppSettings(string keyName, string newKeyValue)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);


            // Attempt to locate the requested setting.
            foreach (XmlElement xmlElementObj in xmlDoc.DocumentElement)
            {
                if (xmlElementObj.Name == "appSettings")
                    foreach (XmlNode xmlNodeObj in xmlElementObj.ChildNodes)
                    {
                        if (xmlNodeObj.Attributes[0].Value == keyName)
                            xmlNodeObj.Attributes[1].Value = newKeyValue;
                    }
            }

            xmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
        }
    }
}
